#!/bin/sh
#################################################################################
# put /app/app/app目录下
#
#################################################################################
cur_dir=$(cd `dirname $0`; pwd)
cd $cur_dir

docker_image_version=3.2.0
if [[ -n $1 ]];then
  docker_image_version=$1
fi
docker_image_flag=capp_app_${docker_image_version}
echo "docker image:$docker_image_flag"

###### 修改配置文件
db_ip=16.77.77.106
db_port=3306
db_name=app
db_user=root
db_pwd=P@ssw0rd123456
##修改application.yml文件
cd $cur_dir/app
application_yml=application.yml
if [[ ! -f application_tpl.yml ]];then
  echo "application_tpl.yml not found"
  exit 1
fi
cat application_tpl.yml>$application_yml

##生成entrypoint.sh文件
cat << EOF > entrypoint.sh
#!/bin/bash
app_ip=127.0.0.1
app_port=7070
db_ip=\`env|grep MYSQL_HOST_IP|awk -F '=' '{print \$NF}'\`
db_port=\`env|grep MYSQL_HOST_PORT|awk -F '=' '{print \$NF}'\`
db_name=\`env|grep MYSQL_DATABASE|awk -F '=' '{print \$NF}'\`
db_user=\`env|grep MYSQL_USER|awk -F '=' '{print \$NF}'\`
db_pwd=\`env|grep MYSQL_PASSWORD|awk -F '=' '{print \$NF}'\`
sed -i "s/APP_DB_USERNAME/\$db_user/g"                           /app/app/app/application.yml
sed -i "s/APP_DB_PASSWORD/\$db_pwd/g"                            /app/app/app/application.yml
sed -i "s/127.0.0.1:3306/\${db_ip}:\${db_port}/g"                /app/app/app/application.yml
sed -i "s/127.0.0.1:8089/\${app_ip}:\${app_port}/g"              /app/app/app/application.yml

touch cmd.conf
echo "jdbc:mysql://\$db_ip:\$db_port/mysql?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&verifyServerCertificate=false&allowPublicKeyRetrieval=true|22|\$db_user|\$db_pwd|key=select count(1) cnt from information_schema.tables where table_schema @eq@ '\$db_name' and table_name like '%sys_%'">/mysql.node
#connect check
while true
do
  java -jar /ops.jar -n /mysql.node -mysql connectCheck >/tmp/connect.log
  connectCnt=\`cat /tmp/connect.log |grep 20|wc -l\`
  echo "Current Connect Status:\$connectCnt"
  d=\`date\`
  echo \$d>>/init.log
  echo "Current Connect Status:\$connectCnt" >>/init.log
  if [ \$connectCnt -gt 0 ]; then
    echo "mysql connect success!"
    break;
  fi
  sleep 5
done

#db check
while true
do
  java -jar /ops.jar -n /mysql.node -mysql exec >/tmp/db.log
  dbCnt=\`cat /tmp/db.log |grep -v select|grep cnt|awk -F ':' '{print \$NF}'\`
  echo "Current Table Count Status:\$dbCnt"
  d=\`date\`
  echo \$d>>/init.log
  echo "Current Table Count Status:\$dbCnt" >>/init.log
  if [ \$dbCnt -gt 40 ]; then
    echo "mysql table success!"
    break;
  fi
  sleep 5
done


java -noverify -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/app/app/app/tmp -jar /app/app/app/app.jar --spring.config.location=/app/app/app/application.yml
EOF

if [[ -f ops.jar ]];then
  rm -rf ops.jar
fi

cp ops.jar.1 ops.jar
##生成Dockerfile文件
cat << EOF > Dockerfile
FROM registry.cn-hangzhou.aliyuncs.com/lank/jdk:s_1.8.342
MAINTAINER APP
USER root
RUN mkdir -p                /app/app/app
RUN mkdir -p                /app/app/app/tmp
RUN mkdir -p                /app/app/app/app_logs
RUN mkdir -p                /app/app/app/lib
RUN mkdir -p                /app/app/app/netdisk
RUN mkdir -p                /app/app/app/upload
ADD ops.jar                 /ops.jar
ADD app.jar                 /app/app/app/app.jar
ADD $application_yml        /app/app/app/application.yml
COPY entrypoint.sh          /entrypoint.sh
RUN chmod +x                /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EOF

##build 生成image
docker build ./ -t registry.cn-hangzhou.aliyuncs.com/lank/app:$docker_image_flag

rm -rf application_tpl_docker.yml
rm -rf entrypoint.sh
rm -rf ops.jar
rm -rf Dockerfile
exit 0


